package com.ls.hnks.controller;

import com.ls.hnks.model.User;
import com.ls.hnks.service.UserService;
import com.ls.hnks.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class ExperienceController {

    private final UserService userService;
    private final List<User> userList;
    private int index;

    @Autowired
    public ExperienceController(UserService userService) {
        this.userService = userService;
        this.userList = new ArrayList<>();
        this.index = 0;
    }

    @RequestMapping("/getAccount")
    public Result getAccount(){
        if(userList.size() < 1){
            List<User> experienceAccount = userService.getExperienceAccount();
            userList.addAll(experienceAccount);
        }
        User account = getOne();
        account.setPassword("123456");
        return Result.ok(account);
    }

    // 提供获取数据的接口
    private User getOne() {
        if(index>=userList.size()){
            index=0;
        }
        return userList.get(index++);
    }
}
